#ifndef COMMON_H
#define COMMON_H

#define NS_PER_SEC 1000000000LL  // 纳秒/秒
#define US_PER_SEC 1000000LL     // 微秒/秒
#define MS_PER_SEC 1000LL        // 毫秒/秒

#include <stdint.h>
#include <stdbool.h>
#include <time.h>

#define REGISTER_COUNT 120
#define MAX_IP_LENGTH 16
#define MAX_FUNCTION_CODES 2
#define JITTER_HISTORY_SIZE 5000
#define TCP_BUFFER_SIZE (1024 * 1024) // 1MB缓冲区
#define MAX_HISTOGRAM_BINS 1000



typedef enum {
    FUNCTION_WRITE_HOLDING_REGISTERS = 16,
    FUNCTION_READ_HOLDING_REGISTERS = 3
} modbus_function_t;

//新的统计
typedef struct 
{
    int64_t min_latency;
    int64_t max_latency;
    uint64_t total_latency;
    uint64_t histogram[MAX_HISTOGRAM_BINS];
    uint64_t over_histogram;
} latency_stats_t;

// 新周期信息结构
typedef struct{
    struct timespec next_period;
    uint64_t period_ns;         // 当前周期
    uint64_t base_period_ns;    // 基础周期
    uint64_t last_adjust_time;  // 最后一次调整周期的时间
    uint64_t max_response_ns;   // 观测到的最大响应时间
    uint64_t adjust_count;      // 周期调整次数统计
    bool need_reset_stats;
    uint64_t last_operation_start;
    
    bool skip_next_interval;    // 跳过下一个间隔统计
} periodic_timer_t;


// 时间统计数据结构
typedef struct {
    // 响应时间统计
    struct {
        int64_t min_response;
        int64_t max_response;
        uint64_t total_response;
        uint64_t response_samples;
    } response;
    
    // 周期间隔统计
    struct {
        int64_t min_interval;
        int64_t max_interval;
        uint64_t total_interval;
        uint64_t interval_samples;
    } interval;
    
    // 周期抖动统计
    struct {
        int64_t min_jitter;
        int64_t max_jitter;
        uint64_t total_jitter;
        uint64_t jitter_samples;
    } jitter;

    struct {
        uint64_t total_cycles;
        uint64_t cycles_with_response;
        uint64_t max_consecutive_misses; // 最大连续无响应周期数
        uint32_t current_consecutive_misses;
    } response_analysis;

    
    // 时钟精度验证
    struct {
        int64_t min_cycle_time;
        int64_t max_cycle_time; 
        uint64_t total_cycle_time;
        uint64_t cycle_samples;
        uint64_t cycle_histogram[MAX_HISTOGRAM_BINS];
    } cycle_stats;
    
    // 直方图数据(单位us)
    uint64_t response_histogram[MAX_HISTOGRAM_BINS];
    uint64_t interval_histogram[MAX_HISTOGRAM_BINS];
    uint64_t over_histogram; // 超过直方图范围的计数
} timing_stats_t;


//实时性配置
typedef struct {
    uint32_t thread_cpu_core;
    int32_t thread_priority;
    bool lock_memory;
    uint32_t tcp_send_buffer_size;
    uint32_t tcp_recv_buffer_size;
    bool disable_tcp_delay_ack;
} rt_config_t;

// 时间函数
uint64_t get_monotonic_time_ns(void);


void init_periodic_timer(periodic_timer_t *timer, uint64_t period_ns);
void wait_next_period(periodic_timer_t *timer);
void update_latency_stats(latency_stats_t *stats, int64_t latency_ns);
void print_latency_stats(const latency_stats_t *stats);

void init_timing_stats(timing_stats_t *stats);
void update_timing_stats(timing_stats_t *stats, 
                        int64_t response_time,
                        int64_t interval_time,
                        int64_t interval_jitter,
                        bool reset_stats);
void print_timing_stats(const timing_stats_t *stats, uint64_t current_period_ns);


void init_cycle_stats(timing_stats_t *stats);
void update_cycle_stats(timing_stats_t *stats, int64_t actual_cycle_time);
void print_cycle_stats(const timing_stats_t *stats);
double calculate_response_time_ratio(const timing_stats_t *stats);
#endif